using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace MKDS_Course_Editor.UI
{
    public partial class NarcFileExplorer : UserControl
    {
        public NarcFileExplorer()
        {
            InitializeComponent();
            toolStrip1.Renderer = new Form2.MyRender();
            imageList1.Images.Add(Properties.Resources.folder_small);
            imageList1.Images.Add(Properties.Resources.page_small);
            imageList1.Images.Add(Properties.Resources.brick_small);
            imageList2.Images.Add(Properties.Resources.folder_big);
            imageList2.Images.Add(Properties.Resources.page_big);
            imageList2.Images.Add(Properties.Resources.brick_big);
            imageList3.Images.Add(Properties.Resources.folder_16);
            listView1.LargeImageList = imageList1;
            treeView1.ImageList = imageList3;
        }

        private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            
        }

        public void RefreshDetails(string Path, int id)
        {
            switch (id)
            {
                case 0:
                    if (listView1.SelectedItems.Count != 0)
                    {
                        pictureBox1.Image = imageList2.Images[listView1.SelectedItems[0].ImageIndex];
                        label1.Text = listView1.SelectedItems[0].Text;
                    }
                    else
                    {
                        pictureBox1.Image = null;
                        label1.Text = "";
                    }
                    break;
            }
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            splitContainer2.Panel2Collapsed = !splitContainer2.Panel2Collapsed;
            //toolStripButton6.Image = (splitContainer2.Panel2Collapsed == false ? Properties.Resources._16758.ToBitmap() : Properties.Resources._16757.ToBitmap());
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            RefreshDetails("", 0);
        }

        private static void PopulateTreeView(TreeView treeView, IEnumerable<string> paths, char pathSeparator)
        {
            TreeNode lastNode = null;
            string subPathAgg;
            foreach (string path in paths)
            {
                    subPathAgg = string.Empty;
                    foreach (string subPath in path.Split(pathSeparator))
                    {
                        subPathAgg += subPath + pathSeparator;
                        TreeNode[] nodes = treeView.Nodes.Find(subPathAgg, true);
                        if (nodes.Length == 0)
                            if (lastNode == null)
                                lastNode = treeView.Nodes.Add(subPathAgg, subPath, 0);
                            else lastNode = lastNode.Nodes.Add(subPathAgg, subPath, 0);
                        else lastNode = nodes[0];
                    }
            }
        }

        private void ParseNode(string node)
        {
            ListViewItem i = new ListViewItem();
            if (node.Contains("."))
            {
                //node.ContextMenuStrip = contextMenuStrip3;
                switch (Path.GetExtension(node).ToLower())
                {
                    case ".nsbtx":
                        i = new ListViewItem(node);
                       // i.ImageIndex = -1;
                        break;
                    case ".nsbmd":
                        i = new ListViewItem(node);
                        i.ImageIndex = 2;
                        //i.ContextMenuStrip = contextMenuStrip2;
                        break;
                    case ".nsbtp":
                    case ".nsbta":
                        i = new ListViewItem(node);
                       // i.ImageIndex = 4;
                        break;
                    case ".nkm":
                        i = new ListViewItem(node);
                       // i.ImageIndex = 5;
                        break;
                    case ".kcl":
                        i = new ListViewItem(node);
                        //i.ImageIndex = 6;
                        break;
                    case ".nclr":
                        i = new ListViewItem(node);
                        //i.ImageIndex = 7;
                        break;
                    case ".ncgr":
                        i = new ListViewItem(node);
                        //i.ImageIndex = 8;
                        break;
                    case ".nscr":
                        i = new ListViewItem(node);
                        //i.ImageIndex = 9;
                        break;
                    case ".ncer":
                        i = new ListViewItem(node);
                        //i.ImageIndex = 10;
                        break;
                    case ".nanr":
                        i = new ListViewItem(node);
                       // i.ImageIndex = 11;
                        break;
                    default:
                        i = new ListViewItem(node);
                        i.ImageIndex = 1;
                        break;
                }
                //if (tv == 1)
                //{
                    //dirpath = path;
                //}
                //else { /*dirpath = path2;*/ }
                if (libWiiSharp.Lz77.IsLz77Compressed(path + "\\" + "unpack\\" + node))
                {
                    i.ForeColor = Color.Blue;
                }
            }
            else
            {
                i = new ListViewItem(node);
                i.ImageIndex = 0;
            }
            listView1.Items.Add(i);
            //foreach (TreeNode child in node.Nodes)
            //    ParseNode(child, tv);

            //TODO: work goes here for DFS 
        }
        string file = @"d:\Temp\mkds test\data\data\KartModelMain.carc";
        string path;
        private void NarcFileExplorer_Load(object sender, EventArgs e)
        {
            if (new FileInfo(file).Extension.ToLower() == ".carc")
            {
                path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\MKDS temp\\" + Guid.NewGuid().ToString();
                System.IO.Directory.CreateDirectory(path);
                File.WriteAllBytes(path + "\\" + new FileInfo(file).Name.Replace("carc", "NARC"), NSMBe4.ROM.LZ77_Decompress(File.ReadAllBytes(file)));
                //lz.Decompress(openFileDialog1.FileName, path);
                System.IO.Directory.CreateDirectory(path + "\\" + "unpack");
                narc.unpack(path + "\\" + new FileInfo(file).Name.Replace("carc", "NARC"), path + "\\" + "unpack");
                //dirpath = path;
                //string[] en = System.IO.Directory.GetFileSystemEntries(path + "\\" + "unpack","*.*", SearchOption.AllDirectories);
                string[] en = System.IO.Directory.GetDirectories(path + "\\" + "unpack", "*.*", SearchOption.AllDirectories);
                for (int i = 0; i < en.Length; i++)
                {
                    en[i] = en[i].Replace(path + "\\" + "unpack", "");
                }
                PopulateTreeView(treeView1, en, '\\');
                treeView1.Nodes[0].Text = "\\";
                //treeView1.Nodes.Clear();
                //TreeNode[] tr = new TreeNode[nc3.Count];
                //nc3.CopyTo(tr, 0);
                //treeView1.Nodes.AddRange(tr);
                string[] en_2 = System.IO.Directory.GetFileSystemEntries(path + "\\" + "unpack", "*.*", SearchOption.TopDirectoryOnly);
                for (int i = 0; i < en_2.Length; i++)
                {
                    en_2[i] = en_2[i].Replace(path + "\\" + "unpack\\", "");
                    ParseNode(en_2[i]);
                }
                //foreach (TreeNode rootNode in treeView1.Nodes)
                //    ParseNode(rootNode, 1);
            }
            else
            {
                path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\MKDS temp\\" + Guid.NewGuid().ToString();
                System.IO.Directory.CreateDirectory(path);
                System.IO.Directory.CreateDirectory(path + "\\" + "unpack");
                System.IO.File.Copy(file, path + "\\" + new FileInfo(file).Name);
                narc.unpack(path + "\\" + new FileInfo(file).Name.Replace("carc", "NARC"), path + "\\" + "unpack");
                //dirpath = path;
                //string[] en_ = System.IO.Directory.GetFileSystemEntries(path + "\\" + "unpack", "*.*", SearchOption.AllDirectories);
                string[] en_ = System.IO.Directory.GetDirectories(path + "\\" + "unpack", "*.*", SearchOption.AllDirectories);
                for (int i = 0; i < en_.Length; i++)
                {
                    en_[i] = en_[i].Replace(path + "\\" + "unpack", "");
                }
                PopulateTreeView(treeView1, en_, '\\');
                //TreeNodeCollection nc3_ = treeView1.Nodes[0].Nodes;
                //treeView1.Nodes.Clear();
                //TreeNode[] tr_ = new TreeNode[nc3_.Count];
                //nc3_.CopyTo(tr_, 0);
                //treeView1.Nodes.AddRange(tr_);
                string[] en_2 = System.IO.Directory.GetFileSystemEntries(path + "\\" + "unpack", "*.*", SearchOption.TopDirectoryOnly);
                for (int i = 0; i < en_2.Length; i++)
                {
                    en_2[i] = en_2[i].Replace(path + "\\" + "unpack", "");
                    ParseNode(en_2[i]);
                }
                //foreach (TreeNode rootNode in treeView1.Nodes)
                //    ParseNode(rootNode, 1);
            }
        }
        
        void TreeView1AfterSelect(object sender, TreeViewEventArgs e)
        {
            listView1.Items.Clear();
            string[] en_2 = System.IO.Directory.GetFileSystemEntries(path + "\\" + "unpack\\"+e.Node.FullPath, "*.*", SearchOption.TopDirectoryOnly);
            for (int i = 0; i < en_2.Length; i++)
            {
                en_2[i] = en_2[i].Replace(path + "\\" + "unpack", "");
                ParseNode(en_2[i]);
                listView1.Items[i].Text = en_2[i].Replace("\\" + e.Node.FullPath, "").Replace("\\", "");
            }
        }
    }
}
